using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.Data.Filtering;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
namespace ChuanPlus.FormControl
{
    public partial class ControlViewInvoice : DevExpress.XtraEditors.XtraUserControl
    {
        public ControlViewInvoice()
        {
            InitializeComponent();
            dateFrom.DateTime = dateTo.DateTime = DateTime.Today;
            GetResult(dateFrom.DateTime, dateTo.DateTime);
        }
        // Provides data for the Total column.
        private void gridView2_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.Column.FieldName == "Total" && e.IsGetData) e.Value =
              getTotalValue(sender as ColumnView, e.ListSourceRowIndex);
        }

        // Returns the total amount for a specific row.
        private decimal getTotalValue(ColumnView view, int listSourceRowIndex)
        {             
            Invoice invoice = (Invoice)gridView1.GetFocusedRow();
            DevExpress.Xpo.XPCollection<InvoiceDetails> xpDetails = invoice.InvoiceDetails;
            InvoiceDetails d = xpDetails[listSourceRowIndex];

            return Convert.ToDecimal(d.UnitPrice * d.Quantity);
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            GetResult(dateFrom.DateTime, dateTo.DateTime);
        }
        private void GetResult(DateTime from, DateTime to)
        {
            xpColInvoices.Criteria = new BetweenOperator("InvoiceDate", from, to);
        }

        private void toolStripMenuPrint_Click(object sender, EventArgs e)
        {
            // Checks whether an end-used has double clicked the row indicator.
            GridHitInfo hi = gridView1.CalcHitInfo(grid.PointToClient(MousePosition));
            if (!hi.InRow) return;

            // get datasource row by row handle
            int DSRowIndex = gridView1.GetDataSourceRowIndex(hi.RowHandle);
            Invoice i = xpColInvoices[DSRowIndex] as Invoice;            
            ChuanPlus.Reports.invoice_master report = new ChuanPlus.Reports.invoice_master();
            report.Parameters[0].Value = i.InvoiceNo;
            report.ShowPreview();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

    }
}
